package com.y.fund.config;

import java.util.Properties;

import javax.sql.DataSource;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;
import com.y.fund.utils.StrUtil;

@Configuration
public class JPAConfig {

    @Autowired
    private Environment environment;


    @Bean("masterDataSource")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource masterDataSource(){
    	return new DruidDataSource();
    }

    @Bean("masterTransactionManager")
    @Primary
    public PlatformTransactionManager masterTransactionManager(@Qualifier("masterSessionFactory") SessionFactory sessionFactory){
        HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager();
        hibernateTransactionManager.setSessionFactory(sessionFactory);
        return hibernateTransactionManager;
    }

    @Bean(name = "masterSessionFactory")
    @Primary
    public LocalSessionFactoryBean sessionFactoryBean(@Qualifier("masterDataSource") DataSource dataSource) {
        LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);
        sessionFactoryBean.setPackagesToScan("com.y.fund.domain");
//        sessionFactoryBean.setEntityInterceptor(new DataLogInterceptor());
        sessionFactoryBean.setHibernateProperties(newHibernateProperties(null));
        return sessionFactoryBean;
    }

    private Properties newHibernateProperties(String sfx) {
        Properties props = new Properties();

        String val = getProp("spring.jpa.properties"+(StrUtil.isBlankIncNullStr(sfx)?"":("."+sfx))+".hibernate.current_session_context_class");
        if(StrUtil.NotBlankIgnoreNullStr(val)) {
            props.setProperty("hibernate.current_session_context_class", val);
        }
        val = getProp("spring.jpa.properties"+ (StrUtil.isBlankIncNullStr(sfx)?"":("."+sfx))+".hibernate.dialect");
        if(StrUtil.NotBlankIgnoreNullStr(val)) {
        	props.setProperty("hibernate.dialect", val);
        }
        val = getProp("spring.jpa.properties"+ (StrUtil.isBlankIncNullStr(sfx)?"":("."+sfx))+".hibernate.hbm2ddl.auto");
        if(null != val) {
        	props.setProperty("hibernate.hbm2ddl.auto", val);
        }
        val = getProp("spring.jpa.properties"+ (StrUtil.isBlankIncNullStr(sfx)?"":("."+sfx))+".hibernate.show-sql");
        if(null != val) {
        	props.setProperty("hibernate.show_sql", val);
        }
        val = getProp("spring.jpa.properties"+ (StrUtil.isBlankIncNullStr(sfx)?"":("."+sfx))+".hibernate.cache.use_second_level_cache");
        if(null != val) {
        	props.setProperty("hibernate.cache.use_second_level_cache", val);
        }
        val = getProp("spring.jpa.properties"+ (StrUtil.isBlankIncNullStr(sfx)?"":("."+sfx))+".hibernate.cache.use_query_cache");
        if(null != val) {
        	props.setProperty("hibernate.cache.use_query_cache", val);
        }
        val = getProp("spring.jpa.properties"+ (StrUtil.isBlankIncNullStr(sfx)?"":("."+sfx))+".hibernate.cache.region.factory_class");
        if(null != val) {
        	props.setProperty("hibernate.cache.region.factory_class", val);
        }
        val = getProp("spring.jpa.properties"+ (StrUtil.isBlankIncNullStr(sfx)?"":("."+sfx))+".hibernate.jdbc.lob.non_contextual_creation");
        if(null != val) {
        	props.setProperty("hibernate.jdbc.lob.non_contextual_creation", val);
        }
        val = getProp("spring.jpa.properties"+ (StrUtil.isBlankIncNullStr(sfx)?"":("."+sfx))+".hibernate.format_sql");
        if(null != val) {
        	props.setProperty("hibernate.format_sql", val);
        }
        val = getProp("spring.jpa.properties"+ (StrUtil.isBlankIncNullStr(sfx)?"":("."+sfx))+".hibernate.validation.mode");
        if(null != val) {
        	props.setProperty("javax.persistence.validation.mode", val);
        }
      
        return props;
    }

    private String getProp(String propKey) {
    	return environment.getProperty(propKey);
    }
}
